<?php
namespace Admin\Controller;
use Think\Controller;
class WeixinController extends Controller {
    //TP构造函数
    public function _initialize(){
        header("Content-type:text/html;charset=utf-8");
        //如果未登录，则跳转到登录页面
        if(!session('admin_user_id')&&!cookie('admin_user_id')){
            echo "<script>window.location.href='/admin/index/login'</script>";exit;
        }
    }
    //微信测试
    public function index(){
        define("TOKEN", "zhaojinyin");
        $echoStr = $_GET["echostr"];
        //验证所有者，通过则进行操作
        if($this->checkSignature()){
            //echo $echoStr;exit;
            echo '验证未通过';exit;
        }
        echo '验证通过';exit;
        /*$data['title']='微信测试 微信 - 找金银网';
        if(I('path.2')!=''){
            $page=I('path.2');
        }else{
            $page=1;
        }
        $perpage=10;
        $Model=D("Order");
        //搜索 关键词
        if(I('post.keyword')){
            $Model->keyword=I('post.keyword');
        }
        //搜索 分类
        /*if(I('post.pc_id')){
            $Model->pc_id=I('post.pc_id');
        }*
        $Model->page=$page;
        $Model->perpage=$perpage;
        $order_list=$Model->get_order_list();
        //print_r($order_list);exit;
        $order_total=$Model->get_order_total();
        //echo $user_total;exit;
        $page_total=ceil($order_total/$perpage);
        //echo $page_total;exit;
        //分页数据
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['perpage']=$perpage;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['order_total']=$order_total;
        $pager['page_total']=$page_total;
        //$cat_list=$Model->get_cat_list();
        $this->assign("order_list", $order_list);
        //$this->assign("cat_list", $cat_list);
        $this->assign("pager",$pager);
        $this->assign("data", $data);*/
        $this->display();
    }
    //确认所有者 验证过后就可以不用每次验证
    private function checkSignature()
    {
        // you must define TOKEN by yourself
        if (!defined("TOKEN")) {
            throw new Exception('TOKEN is not defined!');
        }

        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
    /*
    public function order_edit(){
        $data['title']='编辑订单 交易 - 找金银网';
        $order_id=I('path.2');
        $Model=D("Order");
        $Model->order_id=$order_id;
        $order_info=$Model->get_order_info();
        //print_r($order_info);exit;
        $this->assign("data", $data);
        $this->assign("order_info", $order_info);
        $this->display();
    }
    //
    public function order_edit_post(){
        if($_POST['order_amount']!=''){
            $post=I('post.');
            //print_r($post);exit;
            $Model=D("Order");
            $Model->post=$post;
            if(!$Model->order_edit_post()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>alert('提交成功');window.location.href='/admin/order/index';</script>";
        }else{
            echo "<script>alert('请您填写完整后再尝试重新提交');history.back(-1);</script>";exit;
        }
    }
    //更新排序及批量删除
    public function update_order()
    {
        if(I('post.ids')){
            $ids=I('post.ids');
            $Model=D("Order");
            $Model->ids=$ids;
            if(!$Model->delete_order()){
                echo "<script>alert('删除失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";exit;
        }
        /*elseif(I('post.product_ids')){
            $ids=I('post.product_ids');
            $sorts=I('post.sorts');
            $Model=D("Product");
            $Model->ids=$ids;
            $Model->sorts=$sorts;
            if(!$Model->update_product_sort()){
                echo "<script>alert('更新失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";
        }*
    }
    //
    public function match(){
        $data['title']='撮合管理 交易 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function supply(){
        $data['title']='供货管理 交易 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function supply_add(){
        $data['title']='添加撮合信息 交易 - 找金银网';
        $Product=D("Product");  //产品
        $Member=D("Member");  //会员
        $Brand=D("Brand");  //品牌
        $Spec=D("Spec");  //规格
        $Unit=D("Unit");  //计量单位
        $Depot=D("Depot");  //仓库
        $DeliverTime=D("DeliverTime");  //发货时间
        $DeliverType=D("DeliverType");  //发货类型
        $ShippingType=D("ShippingType");  //物流方式
        $cat_list=$Product->get_cat_list();
        $member_list=$Member->get_member_list();
        $brand_list=$Brand->get_brand_list();
        $spec_list=$Spec->get_spec_list();
        $unit_list=$Unit->get_unit_list();
        $depot_list=$Depot->get_depot_list();
        $deliver_time_list=$DeliverTime->get_deliver_time_list();
        $deliver_type_list=$DeliverType->get_deliver_type_list();
        $shipping_type_list=$ShippingType->get_shipping_type_list();
        //print_r($unit_list);exit;
        $this->assign("data", $data);
        $this->assign("cat_list", $cat_list);
        $this->assign("member_list", $member_list);
        $this->assign("brand_list", $brand_list);
        $this->assign("spec_list", $spec_list);
        $this->assign("unit_list", $unit_list);
        $this->assign("depot_list", $depot_list);
        $this->assign("deliver_time_list", $deliver_time_list);
        $this->assign("deliver_type_list", $deliver_type_list);
        $this->assign("shipping_type_list", $shipping_type_list);
        $this->display();
    }
    //
    public function supply_add_post(){
        if($_POST['supply_title']!=''&&$_POST['supply_desc']!=''){
            $post=I('post.');
            if(!empty($_FILES['supply_image']['tmp_name'])){
                //echo 29929;exit;
                //上传图片
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     3145728 ;// 设置附件上传大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath  =     './data/upload/supply/'; // 设置附件上传根目录
                $upload->savePath  =     ''; // 设置附件上传（子）目录
                $upload->subName='';
                $upload->saveName=md5(time());
                $info   =   $upload->upload();
                if(!$info) {// 上传失败
                    echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
                }
                $supply_image='/data/upload/supply/'.$info['supply_image']['savename'];
                $post['supply_image']=$supply_image;
            }
            $post['supply_desc']=I('post.supply_desc','','');
            $post['supply_time']=time();
            //print_r($post);exit;
            $Model=D("Supply");
            $Model->post=$post;
            if(!$Model->supply_add_post()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>alert('提交成功');window.location.href='/admin/order/supply';</script>";
        }else{
            echo "<script>alert('请您填写完整后再尝试重新提交');history.back(-1);</script>";exit;
        }
    }
    //
    public function virtual(){
        $data['title']='虚拟订单 交易 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function invoice(){
        $data['title']='发票管理 交易 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function refund(){
        $data['title']='退款管理 交易 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function returngoods(){
        $data['title']='退货管理 交易 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function consultation(){
        $data['title']='咨询管理 交易 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function report(){
        $data['title']='举报管理 交易 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function evaluate(){
        $data['title']='评价管理 交易 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function complaint(){
        $data['title']='投诉管理 交易 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }*/
}